# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: TBK <alpine@jjtc.eu>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=bash
pkgver=5.1.16
_patchlevel=${pkgver##*.}
_myver=${pkgver%.*}
_patchbase=${_myver/./}
pkgrel=2
pkgdesc="The GNU Bourne Again shell"
url="https://www.gnu.org/software/bash/bash.html"
arch="all"
license="GPL-3.0-or-later"
makedepends_build="bison flex"
makedepends_host="readline-dev>8 ncurses-dev"
install="$pkgname.post-install $pkgname.post-upgrade $pkgname.pre-deinstall"
subpackages="$pkgname-dbg $pkgname-dev $pkgname-doc"
source="https://ftp.gnu.org/gnu/bash/bash-$_myver.tar.gz
	bashrc
	bash-noinfo.patch
	"
# generate url's to patches. note: no forks allowed!
_i=1
_pad=00
while [ $_i -le $_patchlevel ]; do
	[ $_i -ge 10 ] && _pad="0"
	[ $_i -ge 100 ] && _pad=
	source="$source https://ftp.gnu.org/gnu/bash/bash-$_myver-patches/bash$_patchbase-$_pad$_i"
	_i=$(( _i + 1))
done

# secfixes:
#   4.4.12-r1:
#     - CVE-2016-0634

builddir="$srcdir/$pkgname-$_myver"

export CFLAGS="$CFLAGS -DSYS_BASHRC='\"/etc/bash/bashrc\"'"

prepare() {
	# NOTE: This section is for applying the vendor patches, which are required to fix
	# security holes.  `default_prepare` does *not* apply vendor patches in the format
	# shipped with bash.  We also need to make sure vendor patches are applied before
	# our own.
	# If you disagree, please request an experimental rebuild with bash as /bin/sh,
	# before removing this section.
	for p in $source; do
		case $p in
		*/bash[0-9][0-9]-[0-9]*)
			msg "$p (vendor)"
			patch -p0 -i "$srcdir"/${p##*/}
			;;
		esac
	done

	default_prepare

	# preventively set Version to please 'apk version --check'
	sed -i "s/@PACKAGE_VERSION@/$pkgver/" "$builddir"/support/bash.pc.in
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--bindir=/bin \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-curses \
		--disable-nls \
		--enable-readline \
		--without-bash-malloc \
		--with-installed-readline
	# parallel build workarounds
	make y.tab.c && make builtins/libbuiltins.a && make
}

check() {
	make test
}

package() {
	mkdir -p "$pkgdir"/etc/bash
	make DESTDIR="$pkgdir" install
	install -Dm644  "$srcdir"/bashrc "$pkgdir"/etc/bash/bashrc
	rm -rf "$pkgdir"/usr/share/locale
}

dev() {
	default_dev
	mkdir -p "$subpkgdir"/bin "$subpkgdir"/usr/lib/$pkgname
	mv "$pkgdir"/bin/${pkgname}bug "$subpkgdir"/bin
	mv "$pkgdir"/usr/lib/$pkgname/Makefile* "$subpkgdir"/usr/lib/$pkgname
}

sha512sums="
c44a0ce381469219548a3a27589af3fea4f22eda1ca4e9434b59fc16da81b471c29ce18e31590e0860a6a251a664b68c2b45e3a17d22cfc02799ffd9a208390c  bash-5.1.tar.gz
4542e19f2c6c31fff1f412379d5e36c65767cf2d91f1311e8d43f2566e2e50ab8dddff6045aec9b0272ed7387aa9b35daf6b0844478c0bda94d961efcf6f3ae3  bashrc
9d8845491d0fe335bdd8e9a2bd98bda54bfed2ae3c35b2196c6d5a38bdf96c4d97572ba7d6b19ab605ef4e8f001f64cf3312f87dedebb9e37a95ad2c44e33cdb  bash-noinfo.patch
1cd86805a2639614372aec29a710bc456e330abcbbaa0867820c94f714a1fa5fb5c1b18aa2c10263ae0bce9dad7579c7af2f732282315c1c34bfd6a90777bfd2  bash51-001
923e7822a9629645347d3aea0058fb5e2d52223507159a62369309f264612df44a84931c19e0ccb3852e98ce672dfbd454477090b4041b5a0de477c94eb61088  bash51-002
01e952dcfdae58624723d64912ea3444eed2fdcd266ba1a929b95ec3abd70f914bf400607c3f7bb7a94ac2925f794f91f37c1929d5bb987de2ba7f60a19cb8bd  bash51-003
10ff24cd91a2cd88818bfa7218050843af6b409e43fcca89f5ec70d8266020c6c2a55132426271f165cd0f154f49eb0f8ec2761b80fc066c921b83120bb543ce  bash51-004
fa83d894fe874a05b9a7d47b8bca8e5b7f4067221d82e8b1af616d17725592c3737c621f2a8ad3c917b29846012c37c85acd34dcbb43eb6b05065ccce89b260c  bash51-005
b9b6e3d71f7b7718e2e8598ec8e337dcc675571fb233c29e5230ebf14eab2249204531f2fe8c4d1459c5fed10acb679048588d1e457e98dbc00ffc4d2cd227e3  bash51-006
e4ebdc47e780ddc2588ecdfcfe00cb618039c7044e250ab2b836b0735c461ebacd15beaf2145e277c70b7f51cded55bd8dde7757df810f33f8dae306ee5ba571  bash51-007
97f9558a08a66cc9da62c285bf9118b39328e25ed3b9277728e0539b1ac0adef176a090e39cd96dc03d6fd900d8155bd58040cb3390a09f637bab1de8af3faf6  bash51-008
2d3c65162ec4e5c3dfeb439891950ef2c43973a84122fcdf6b56c388466c7e671dbc9b236d2253f01411b668c365855263995dbacb8e6f9e9dbcb7e6c2cc518c  bash51-009
aac4a0b72b559566334f1029c52754f4c98185af99e09436e401d83ab81bab7882d0d8050674b30f171733f3628157777a264566e927e93db2ea5a18d26630f1  bash51-010
bb9e47a570bb9758c365831f9650b9379b60862b8cef572edc3cd833df96ebb8b9612de474bdc2a03ff4efc2275f871d55962295385e38f3658874488e974b81  bash51-011
59819914b6821d9f4af0aade7b9b7ea92368c2b8eb8407cea11dfeee7208905dd06bdef7a049d7b1c4fac41c44d9a130b95a061957a9649050b37471b3044cf1  bash51-012
67535155f49a7f54f151e62aba9274f82d01f33a1a1a7e5efd1aa0d63ba2d078765f0b5e22cb24db7132eff2d8c5852a3688298baa5217b8b6e159aae065d748  bash51-013
f658ab7ef01ba1d26f735e24b23bf35687e15b0d5d20f90da233d000745a55bdba142c11e9fba52e3b84470ec625fab60cc74cd6be533d990496a3795c658e88  bash51-014
fd4bc85f942a3a16c545f7e951a24f620ff2d884640dea6e05f305aaf88ed41862bfb05eea2258881608de696f9dc7a0fe3bebb51a011f50b720ea7a66699184  bash51-015
020b3f3db77ca603a27a3423323538db5c9844be17ee428cf7cda80bebdcc715d30eab6c95773541cb8d14f3ad9e6142bf0adcda0e745ee638242508cc0ab05f  bash51-016
"
